package com.zrt.pad.commonui.task;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ImageSpan;
import android.util.Log;

import com.zrt.pad.commonui.PromptWindow;
import com.zrt.pad.commonui.WaitBoard;
import com.zrt.pad.datasource.DataSourceHelper;
import com.zrt.pad.padpos.R;

public class SyncTask extends AsyncTask<Double, Integer, Boolean> {

	public static boolean hasInstanceRunning = false;

	public AsyncTask<Void, Void, Boolean> successTask;
	public AsyncTask<Void, Void, Boolean> failedTask;

	public static final int FLAG_START = 1;
	public static final int FLAG_SUCCESS = 2;
	public static final int FLAG_FAILED = 3;
	public static final int FLAG_INIT = 4;

	private Activity activity;
	private ImageSpan loading1;
	private ImageSpan ok1;
	private ImageSpan clear1;
	private ImageSpan error1;

	private String infoText = "数据同步中...   [1]";
	private int ds1;
	private SpannableString info;

	private String errorMsg = "同步失败！";

	public SyncTask(Activity ac) {
		super();
		activity = ac;
	}

	@Override
	protected void onPreExecute() {
		super.onPreExecute();
		hasInstanceRunning = true;
		WaitBoard.instance.show(activity);
	}

	@Override
	protected Boolean doInBackground(Double... params) {
		// init
		ds1 = infoText.indexOf("[1]");
		info = new SpannableString(infoText);
		Drawable dloading = activity.getResources().getDrawable(
				R.drawable.loading_animation1);
		Drawable dok = activity.getResources().getDrawable(
				R.drawable.img_menu_detail_list_check);
		Drawable dclear = activity.getResources().getDrawable(R.drawable.clear);
		Drawable derror = activity.getResources().getDrawable(
				R.drawable.table_row_delete);
		dloading.setBounds(0, 0, 20, 20);
		dok.setBounds(0, 0, 20, 20);
		dclear.setBounds(0, 0, 20, 20);
		derror.setBounds(0, 0, 20, 20);
		loading1 = new ImageSpan(dloading);
		ok1 = new ImageSpan(dok);
		clear1 = new ImageSpan(dclear);
		error1 = new ImageSpan(derror);
		publishProgress(FLAG_INIT);
		publishProgress(FLAG_START);
		
		// 开始业务
		boolean result = false;
		boolean result1 = false;
		boolean result2 = false;
		try {

			ConnectivityManager con = (ConnectivityManager) activity
					.getSystemService(Context.CONNECTIVITY_SERVICE);
			NetworkInfo networkinfo = con.getActiveNetworkInfo();
			if (networkinfo == null || !networkinfo.isAvailable()) {
				// 当前网络不可用
				errorMsg = "网络未连接！";
				publishProgress(FLAG_FAILED);
				result = false;
			} else {
				result1 = DataSourceHelper.initSysOption();
				result2 = DataSourceHelper.initUsrUser();
				Log.i("result", result1 + "/" + result2 + "/");
				result = result1 && result2;
				
				if (result) {
					publishProgress(FLAG_SUCCESS);
				} else {
					publishProgress(FLAG_FAILED);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			errorMsg = e.getMessage();
			publishProgress(FLAG_FAILED);
			result = false;
		}
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		return result;
	}

	@Override
	protected void onProgressUpdate(Integer... values) {
		super.onProgressUpdate(values);
		int v = values[0];
		switch (v) {
		case FLAG_START: {
			info.removeSpan(clear1);
			info.setSpan(loading1, ds1, ds1 + 3,
					Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
		}
			break;
		case FLAG_SUCCESS: {
			info.removeSpan(loading1);
			info.setSpan(ok1, ds1, ds1 + 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
			
		}
			break;
		case FLAG_FAILED: {
			info.removeSpan(loading1);
			info.setSpan(error1, ds1, ds1 + 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
			//Toast.makeText(activity, errorMsg, Toast.LENGTH_LONG).show();
			
			PromptWindow PW = new PromptWindow(activity, errorMsg);
			PW.showDialog();
		}
			break;
		case FLAG_INIT: {
			info.setSpan(clear1, ds1, ds1 + 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
		}
		default:
			break;
		}
		WaitBoard.instance.setInformation(info);
	}

	@Override
	protected void onPostExecute(Boolean result) {
		super.onPostExecute(result);
		if (result) {
			if (successTask != null) {
				successTask.execute();
			}
		} else {
			if (failedTask != null) {
				failedTask.execute();
			}
		}
		activity = null;
		WaitBoard.instance.hide();
		hasInstanceRunning = false;
	}
}
